<?php
    class admin_getLinks_controller extends controllerBase
    {
        public function process()
        {
            $oDatabase = new database();
            if(isset($_POST['tb_provider']))
            {
                $sQuery = "SELECT * FROM provider WHERE id='".$_POST['tb_provider']."'";
                $aProvider = $oDatabase->getObject($sQuery);
                // Create DOM from URL
                $html = file_get_html($aProvider->provider_url);

                // Find all item blocks
                $iCount = 0;
                foreach($html->find($aProvider->item_link_sector) as $aItem) {
                    if (filter_var($aItem->href, FILTER_VALIDATE_URL)) {
                        $sUrl = $aItem->href;
                    }
                    else 
                    {
                        $sUrl = $aProvider->host.$aItem->href;
                    }
                    $sQuery = "SELECT * FROM product_list_url WHERE url ='".trim($sUrl)."'";
                    $result = $oDatabase->getObject($sQuery);
                    if(!$result)
                    {
                        $aData = array(
                            'url' => trim($sUrl),
                            'status' => 0,
                            'provider_id' => $aProvider->id,
                        );

                        if($oDatabase->add('product_list_url',$aData))
                        {
                            $iCount += 1;
                        }
                    }
                }

                $this->aParams['message'] = 'Get '.$iCount.' link products into database!';
            }
        }
    }
?>
